package com.qiniuyun.backai.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.qiniuyun.backai.common.BaseResponse;
import com.qiniuyun.backai.common.Constant;
import com.qiniuyun.backai.common.ErrorCode;
import com.qiniuyun.backai.common.ResultUtils;
import com.qiniuyun.backai.exception.ThrowUtils;
import com.qiniuyun.backai.model.dto.UserDto;
import com.qiniuyun.backai.model.pojo.User;
import com.qiniuyun.backai.service.UserService;
import com.qiniuyun.backai.util.CacheUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Autowired
    private CacheUtil cacheUtil;
    private final UserService userservice;

    public UserController(UserService userservice) {
        this.userservice = userservice;
    }

    @PostMapping("/register")
    public BaseResponse<Integer> register(@RequestBody User user) {
        //判空
        ThrowUtils.throwIf(user == null, ErrorCode.PARAMS_ERROR);
        Integer a = userservice.register(user);
        //1 成功 0 失败
        return ResultUtils.success(a);
    }

    @PostMapping("/login")
    public BaseResponse<String> login(@RequestBody UserDto userDto, HttpServletRequest request) {
        //判空
        ThrowUtils.throwIf(userDto == null, ErrorCode.PARAMS_ERROR);
        String username = userDto.getUsername();
        String password = userDto.getPassword();
        QueryWrapper<User> wrapper = new QueryWrapper<User>().eq("username", username);
        User user = userservice.getOne(wrapper);
        if (user == null) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR, "未注册，请您先注册");
        }
        String s = DigestUtil.md5Hex(password);
        if (!s.equals(user.getPassword())) {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR, "密码错误，请重新输入");
        }
        userDto.setPassword(s);
        request.getSession().setAttribute(Constant.SESSION_KEY, userDto);
        cacheUtil.map.put(username, user);
        return ResultUtils.success("登录成功");
    }

    @DeleteMapping("/delete")
    public BaseResponse<Boolean> deleteUser(HttpServletRequest request) {
        UserDto userDto = (UserDto)request.getSession().getAttribute(Constant.SESSION_KEY);
        String password = userDto.getPassword();
        String username = userDto.getUsername();
        UpdateWrapper<User> wrapper = new UpdateWrapper<User>()
                .eq("username", username)
                .eq("password", password);
        boolean remove = userservice.remove(wrapper);
        //缓存删除
        cacheUtil.map.remove(username);
        request.getSession().removeAttribute(username);
        return  ResultUtils.success(remove);
    }

    @PutMapping("/update")
    public BaseResponse<Boolean> updateUser(@RequestParam String password,HttpServletRequest request) {
        //加密
        password=DigestUtil.md5Hex(password);
        UserDto userDto = (UserDto)request.getSession().getAttribute(Constant.SESSION_KEY);
        String username = userDto.getUsername();
        UpdateWrapper<User> wrapper = new UpdateWrapper<User>()
                .eq("username", username)
                .set("password",password);
        boolean update = userservice.update(wrapper);
        if (update){
            QueryWrapper<User> wrapper1 = new QueryWrapper<User>().eq("username", username);
            User user = userservice.getOne(wrapper1);
            user.setPassword(password);
            userDto.setPassword(password);
            cacheUtil.map.put(username,user);
            request.getSession().setAttribute(Constant.SESSION_KEY, userDto);
        }
        return  ResultUtils.success(update);
    }
}
